package com.raincat.tools.nettools;

import android.content.Context;
import android.net.Uri;

import androidx.annotation.NonNull;

import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 *     author : RainCat
 *     org    : Shenzhen JingYu Network Technology Co., Ltd.
 *     e-mail : nining377@gmail.com
 *     time   : 2020/03/23
 *     desc   : http配置
 *     version: 1.0
 * </pre>
 */
public class HttpConfig {
    static final String HTTP = "http";
    static final String HTTPS = "https";

    private static final String POST = "POST";
    static final String GET = "GET";

    ConnectHttp connectHttp;
    ConnectHttps connectHttps;
    Request request;

    public HttpConfig() {
        request = new Request();
        request.requestMode = HTTP;
        request.cookie = NetConfig.cookie;
        request.reTry = NetConfig.reTry;
        request.connectTimeout = NetConfig.connectTimeout;
        request.readTimeout = NetConfig.readTimeout;
    }

    public HttpConfig doPost(@NonNull String url, @NonNull String params) {
        request.method = POST;
        request.url = url;
        request.param = params;
        return this;
    }

    public HttpConfig doPost(@NonNull String url, @NonNull HashMap<String, Object> params) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            if (entry.getValue() == null || entry.getValue().toString().length() == 0)
                continue;
            stringBuilder.append(entry.getKey());
            stringBuilder.append("=");
            stringBuilder.append(Uri.encode(entry.getValue().toString(), "UTF-8"));
            stringBuilder.append("&");
        }
        if (stringBuilder.length() != 0)
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);

        return doPost(url, stringBuilder.toString());
    }

    public HttpConfig doGet(@NonNull String url) {
        request.method = GET;
        request.url = url;
        return this;
    }

    public HttpConfig doGet(@NonNull String url, @NonNull HashMap<String, Object> params) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            if (entry.getValue() == null || entry.getValue().toString().length() == 0)
                continue;
            stringBuilder.append(entry.getKey());
            stringBuilder.append("=");
            try {
                stringBuilder.append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            stringBuilder.append("&");
        }
        if (stringBuilder.length() != 0) {
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            request.url = url + "?" + stringBuilder.toString();
        } else
            request.url = url;

        request.method = GET;
        return this;
    }

    /**
     * 请求头
     *
     * @param headers
     * @return
     */
    public HttpConfig headers(@NonNull HashMap<String, Object> headers) {
        request.headers = headers;
        return this;
    }

    /**
     * 是否显示加载框
     *
     * @param isShowDialog
     * @param dialogMessage
     * @return
     */
    public HttpConfig showLoadingDialog(boolean isShowDialog, @NonNull String dialogMessage) {
        if (isShowDialog)
            request.dialogMessage = dialogMessage;
        return this;
    }

    public void start(Context context, NetCallBack back) {
        if (request.url == null || request.url.length() == 0)
            throw new IllegalArgumentException("url can't be null");
        request.context = new WeakReference<>(context);
        request.back = back;
        if (request.requestMode.equals(HTTP)) {
            connectHttp = new ConnectHttp();
            connectHttp.init(request);
        } else if (request.requestMode.equals(HTTPS)) {
            connectHttps = new ConnectHttps();
            connectHttps.init(request);
        }
    }

    public void cancel() {
        if (request.requestMode.equals(HTTP) && connectHttp != null) {
            connectHttp.cancel();
        } else if (request.requestMode.equals(HTTPS) && connectHttps != null) {
            connectHttps.cancel();
        }
    }
}
